package com.liuxu.topnews.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.floating.FloatingManager;
import com.liuxu.topnews.floating.FloatingView;
import com.liuxu.topnews.utils.LogUtil;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import static com.liuxu.topnews.MyApplication.autoclear;
import static com.liuxu.topnews.MyApplication.cycle_time;
import static com.liuxu.topnews.MyApplication.float_window;
import static com.liuxu.topnews.MyApplication.globalapp;
import static com.liuxu.topnews.MyApplication.globalapps;
import static com.liuxu.topnews.MyApplication.permission;
import static com.liuxu.topnews.MyApplication.sleep;

public class FloatWindowService extends Service{
    private static FloatWindowService Instance;
    private Timer timer;
    private String display_app_name;
    private int display_time;
    private int display_total_time;
    private int clear_day;
    /**
     * 用于在线程中创建或移除悬浮窗。
     */
    private Handler handler = new Handler();

    public static FloatWindowService getInstance() {
        return Instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Instance=FloatWindowService.this;
        LogUtil.getInstance().show("Service", "onCreate");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.getInstance().show("Service", "onDestroy");// Service被终止的同时也停止定时器继续运行
        timer.cancel();
        timer = null;

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        clear_day=100;
        if (float_window){
            FloatingManager.getInstance().show();
        }
        // 开启定时器，每隔0.5秒刷新一次
        if (timer == null) {
            timer = new Timer();
            timer.scheduleAtFixedRate(new RefreshTask(), 0, 1000);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void updateData(){
        try {
            if (!globalapp.zname.equals(display_app_name)){
                display_app_name=globalapp.zname;
            }
            if (cycle_time!=display_time){
                display_time=cycle_time;
                display_total_time=ShareUtil.getInstance().AppReadCountGet(globalapp);
            }
            String appname= "当前:"+display_app_name;
            String total_time= "总共已读:"+display_total_time+"篇";
            String updata_time="本轮还剩"+display_time+"篇";
            if (float_window){
                FloatingManager.getInstance().updateData(appname,total_time,updata_time);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    class RefreshTask extends TimerTask {

        @Override
        public void run() {
            if (permission){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        updateData();
                    }
                });
            }
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
            int ap=cal.get(Calendar.AM_PM);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int hour= cal.get(Calendar.HOUR);
            int minute =cal.get(Calendar.MINUTE );
            int second  = cal.get(Calendar.SECOND );
            if (clear_day==100){
                clear_day=day;
            }
//        Log.i("现在时间", hour+"时"+minute+"分"+second+"秒");
            if (ap==0&& sleep){
//                Log.i("睡眠功能", "进入判断");
                if (hour==1&&!MyApplication.Pause){
                    Log.i("时间到", "暂停");
                    MyApplication.Pause=true;
                    ToastUtils.getInstance().shownow("凌晨开始休息");
                }
                if (hour==5&&MyApplication.Pause){
                    Log.i("时间到", "继续");
                    MyApplication.Pause=false;
                    ToastUtils.getInstance().shownow("起床开始赚钱");
                }
            }
//            Log.i(String.valueOf(day), String.valueOf(clear_day));
            if (ap ==1&&hour==11&&minute==59&&autoclear&&day==clear_day){
                clear_day+=1;
                ShareUtil.getInstance().celardata(globalapps);
                Log.i("时间到", "阅读数据已重置");
                ToastUtils.getInstance().shownow("阅读数据已重置！");
            }
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
